package com.gts.sofa.pojo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

/**
 *  自定义的组件想要使用spring 容器底层的一些组件 例如ApplicationContext BeanFactory,等等）只需要自定义的组件实现
 *   XXXAware接口就行
 *
 *   aware,翻译过来是知道的，已感知的，意识到的，所以这些接口从字面意思应该是能感知到所有Aware前面的含义。
 *
 *   先举个BeanNameAware的例子，实现BeanNameAware接口，
 *   可以让该Bean感知到自身的BeanName（对应Spring容器的BeanId属性）属性
 *
 *
 */
@Component
public class Red implements ApplicationContextAware,BeanNameAware,EmbeddedValueResolverAware {

    private ApplicationContext applicationContext;




    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        this.applicationContext = applicationContext;




        System.out.println(applicationContext);
    }

    public void test(){
        Car car = applicationContext.getBean(Car.class);
        car.setColor("yellow");

    }

    @Override
    public void setBeanName(String name) {
        System.out.println("当前bean的名字是"+name);

    }


    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        System.out.println(resolver.resolveStringValue("你好${os.name}"));
    }


}
